#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
from lxml import etree
import logging

from cvirt.cnode.libvirtXmlObject.baseDomain import XmlObject

LOG = logging.getLogger(__name__)

"""
    @author jay.han
"""

class GuestSMBIOS(XmlObject):
    ROOT_NAME = 'smbios'
    def __init__(self, parse_xml=None, **kw):
        super(GuestSMBIOS, self).__init__(parse_xml=parse_xml,**kw)

        self.mode = "sysinfo"

    def _get_xml_config(self, root):
        smbios = root
        smbios.set("mode", self.mode)

        return smbios


class GuestSysinfo(XmlObject):
    ROOT_NAME = 'sysinfo'
    def __init__(self, parse_xml=None, **kw):
        super(GuestSysinfo, self).__init__(parse_xml=parse_xml,**kw)

        self.type = "smbios"
        self.bios_vendor = 'morningcloud'
        self.bios_version = '1.0'
        self.system_manufacturer = 'Tcloud'
        self.system_product = '2.2'
        self.system_version = '2.2'
        self.system_serial = '2.2'
        self.system_uuid = None

        self._set_attr(**kw)

    def _parse_xml(self, xmldoc):
        self.type = xmldoc.get('type')
        for c in xmldoc.getchildren():
            if c.tag == 'bios':
                self.parse_bios(c)
            elif c.tag == 'system':
                self.parse_system(c)

    def parse_bios(self,xmldoc):
        for c in xmldoc.getchildren():
            if c.get('name') == 'vendor':
                self.bios_vendor = c.text
            elif c.get('name') == 'version':
                self.bios_version = c.text

    def parse_system(self,xmldoc):
        for c in xmldoc.getchildren():
            if c.get('name') == 'manufacturer':
                self.system_manufacturer = c.text
            elif c.get('name') == 'product':
                self.system_product = c.text
            elif c.get('name') == 'version':
                self.system_version = c.text
            elif c.get('name') == 'serial':
                self.system_serial = c.text
            elif c.get('name') == 'uuid':
                self.system_uuid = c.text

    def _get_xml_config(self, root):
        root.set("type", self.type)

        bios = None
        system = None

        if self.bios_vendor is not None:
            if bios is None:
                bios = etree.Element("bios")
            info = etree.Element("entry", name="vendor")
            info.text = self.bios_vendor
            bios.append(info)

        if self.bios_version is not None:
            if bios is None:
                bios = etree.Element("bios")
            info = etree.Element("entry", name="version")
            info.text = self.bios_version
            bios.append(info)

        if self.system_manufacturer is not None:
            if system is None:
                system = etree.Element("system")
            info = etree.Element("entry", name="manufacturer")
            info.text = self.system_manufacturer
            system.append(info)

        if self.system_product is not None:
            if system is None:
                system = etree.Element("system")
            info = etree.Element("entry", name="product")
            info.text = self.system_product
            system.append(info)

        if self.system_version is not None:
            if system is None:
                system = etree.Element("system")
            info = etree.Element("entry", name="version")
            info.text = self.system_version
            system.append(info)

        if self.system_serial is not None:
            if system is None:
                system = etree.Element("system")
            info = etree.Element("entry", name="serial")
            info.text = self.system_serial
            system.append(info)

        if self.system_uuid is not None:
            if system is None:
                system = etree.Element("system")
            info = etree.Element("entry", name="uuid")
            info.text = self.system_uuid
            system.append(info)

        if bios is not None:
            root.append(bios)
        if system is not None:
            root.append(system)

        return root